#include "radarwidget.h"
#include "./ui_radarwidget.h"

#include <QPainter>
#include <QTimer>

RadarWidget::RadarWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::RadarWidget)
{
    ui->setupUi(this);

    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, [=]()
    {
        angle += 5;
        if(angle > 360)
            angle = 0;
        update();
    });
    timer->start(50);
}

RadarWidget::~RadarWidget()
{
    delete ui;
    delete timer;
}

void RadarWidget::paintEvent(QPaintEvent * event) {

    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing, true);

    //绘制黑色背景
    painter.setBrush(Qt::black);
    painter.drawRect(this->rect());

    //开始画圆圈,每个圆圈之间的间隔
    int side = qMin(width(), height())/2/7;

    //坐标系挪到屏幕的中心
    painter.translate(rect().center());
    painter.setPen(QPen(Qt::green, 4));
    painter.setBrush(Qt::NoBrush);
    for(int i=1;i<=7; i++)
    {
        painter.drawEllipse( QPoint(0,0), side*i, side*i);
    }

    //X,Y 轴
    painter.drawLine(-side*7,0, side*7,0);
    painter.drawLine(0,-side*7, 0,side*7);


    //锥型
    QConicalGradient co(0,0,-angle);
    co.setColorAt(0, QColor(0,255,0,200));
    co.setColorAt(0.1, QColor(0,255,0,100));
    co.setColorAt(0.2, QColor(0,255,0,0));
    co.setColorAt(1, QColor(0,255,0,0));

    painter.setPen((Qt::NoPen));
    painter.setBrush(co);
    painter.drawPie(-qMin(width(), height())/2, -qMin(width(), height())/2, qMin(width(), height()), qMin(width(), height()),
                    -angle*16, 72*16);

}

